package com.jintoufs.service.scene.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.scene.SceneOrderDetailMapper;
import com.jintoufs.dao.scene.SceneOrderMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.scene.SceneOrderDetail;
import com.jintoufs.domain.scene.SceneTicket;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.service.weixin.WxRefundService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by pengyang on 2017/12/30.
 */
@Service("sceneOrderService")
public class SceneOrderServiceImpl implements SceneOrderService {

    @Resource
    private SceneOrderMapper sceneOrderMapper;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    private ShopService shopService;

    @Resource
    private SceneOrderDetailMapper sceneOrderDetailMapper;
    @Resource
    private SceneTicketService sceneTicketService;

    @Resource
    private WxRefundService refundService;
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private AccountDetailService accountDetailService;

    @Override
    public SceneOrder saveSceneOrder(SceneOrder sceneOrder) {
        sceneOrderMapper.insertSelective(sceneOrder);
        return sceneOrder;
    }

    @Override
    public SceneOrderDetail saveSceneOrderDetail(SceneOrderDetail sceneOrderDetail) {
        sceneOrderDetailMapper.insertSelective(sceneOrderDetail);
        return sceneOrderDetail;
    }

    @Override
    public SceneOrder updateOrder(SceneOrder sceneOrder) {
        sceneOrder.setIsNew(Constants.ORDER_IS_NEW);
        sceneOrderMapper.updateByPrimaryKeySelective(sceneOrder);
        return sceneOrder;
    }

    @Override
    public SceneOrder getOrderByTransactionId(String transactionId) {
        return sceneOrderMapper.selectOrderByTransactionId(transactionId);
    }

    @Override
    public List<Map<String, Object>> selectByParam(Map<String, Object> param) {
        return sceneOrderMapper.getByParams(param);
    }

    @Override
    public List<Map<String, Object>> selectByFilter(QueryFilters filters) {
        return sceneOrderMapper.selectByFilter(filters);
    }

    @Override
    public SceneOrder getById(String id) {
        return sceneOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void payConfirm(String orderId) {
        SceneOrder sceneOrder = sceneOrderMapper.selectByPrimaryKey(orderId);
        sceneOrder.setStatus(Constants.ORDER_STATUS_CONFIRM);
        sceneOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
        sceneOrder.setModifyTime(new Date());
        sceneOrder.setIsNew(Constants.ORDER_IS_NEW);
        sceneOrderMapper.updateByPrimaryKeySelective(sceneOrder);


        Map<String,Object> params = new HashMap<>();
        params.put("id",orderId);
        List<Map<String,Object>> list =  getByParam(params);
        if (ValidUtil.isEmpty(list)){
            return;
        }
        Integer smsId;
        ArrayList<String> smsParams = new ArrayList<String>();

        smsId = Integer.valueOf(dictionaryService.getDicByKeyNo("SCENE_CONFIRM_SMS").get(0).getCaption());
        if (!ValidUtil.isEmpty(list)){
            Object object;

            List<SceneOrderDetail> sceneOrderDetails=sceneOrderDetailMapper.selectBySceneOrderId(sceneOrder.getId());
            if(sceneOrderDetails!=null&&sceneOrderDetails.size()>0){
                String sceneTricketId=sceneOrderDetails.get(0).getSceneTricketId();
                SceneTicket sceneTicket= sceneTicketService.queryById(sceneTricketId);
                if(sceneTicket!=null){
                    smsParams.add(sceneTicket.getTicketName());
                }
            }

            object = sceneOrder.getId().substring(sceneOrder.getId().length()-14,sceneOrder.getId().length());
            smsParams.add(object.toString());
            object = list.get(0).get("telephone");
            if (!ValidUtil.isEmpty(object)){
                String telephone = list.get(0).get("telephone").toString();
                refundService.sendOrderSms(telephone,smsId,smsParams);
            }
        }
    }

    public String getUUID(){
        return String.valueOf(snowflakeIdWorker.nextId());
    }
    @Override
    public Map<String,List<String>> getIndexData(Integer num,String customerId) {
        Map<String,List<String>> sceneIndex = new HashMap<>();
        Date endDate=new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -num);
        c.set(Calendar.DAY_OF_MONTH,1);
        Date startDate = c.getTime();
        Map<String,Object> param = new HashMap<>();
        param.put("endDate", endDate);
        param.put("startDate", startDate);
        if (!ValidUtil.isEmpty(customerId)){
            param.put("customerId",customerId);
        }
        sceneIndex.put("orderNumber",sceneOrderMapper.orderIndex(param));
        sceneIndex.put("orderAmount",sceneOrderMapper.orderPriceIndex(param));

        sceneIndex.put("orderDate",sceneOrderMapper.orderDateIndex(param));
        return sceneIndex;
    }

    @Override
    public List<Map<String, Object>> getByParam(Map<String, Object> param) {
        return sceneOrderMapper.getOrderInfoByParams(param);
    }

    @Override
    public Map getOrderPriceAndCostByOrderId(String orderId) {
        return sceneOrderMapper.getOrderPriceAndCostByOrderId(orderId);
    }

    @Override
    public List<SceneOrder> getNoConfirm(Map<String, Object> param) {
        return sceneOrderMapper.getNoConfirm(param);
    }

    @Override
    public int updateOrderNew(Map param) {
        return sceneOrderMapper.updateOrderNew(param);
    }
}
